
package com.benimetum.beans;

import com.benimetum.items.*;
import com.benimetum.managers.*;
import com.benimetum.managers.LoginManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.*;



/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
@ManagedBean(name="HomePageBean")
@SessionScoped
public class HomePageBean 
{
    private String girisButtonLabel;
    private boolean loginPanel;
    private String username;
    private String password;
    private boolean logon;
    private boolean errorLogin;
    private String errorMessage;
    private String sayfaalt;
    private String sayfaBox;
    private String boxTitle;
    private boolean box;
    private boolean status;
    private ScheduleModel eventModel;  
    private Person user;
    private Course[] courses;
    private int poolId;
    
    public HomePageBean() {
	  girisButtonLabel="Giriş";
	  loginPanel=false;
	  errorLogin=false;
          username="";
          status=true;
          password="";
          poolId=100;
	  if(user==null)
              logon=false;
          else
              logon=true;
	  eventModel=new DefaultScheduleModel();
          
    }

    public void setPoolId(int poolId) {
        this.poolId = poolId;
    }

    public int getPoolId() {
        return poolId;
    }

    public String getPassword() {
	  return password;
    }

    public boolean getBox() {
	  return box;
    }

    public boolean getStatus() {
        return status;
    }

    public Course[] getCourses() {
        return courses;
    }

    public String getSayfaBox() {
	  return sayfaBox;
    }
    
    public String getErrorMessage() {
	  return errorMessage;
    }

    public Person getUser() {
	  return user;
    }

    public String getSayfaalt() {
	  return sayfaalt;
    }

    public boolean getLogon(){
	  return logon;
    }

    public ScheduleModel getEventModel() {
	  return eventModel;
    }

    public String getBoxTitle() {
	  return boxTitle;
    }

    public void setCourses(Course[] courses) {
        this.courses = courses;
    }
    
    public String getUsername() {
	  return username;
    }
    
    public boolean getLoginPanel(){
	  return loginPanel;
    }
    
    public boolean getErrorLogin(){
	  return errorLogin;
    }
    
    public String getGirisButtonLabel() {
	  return girisButtonLabel;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public void setGirisButtonLabel(String girisButtonLabel) {
	  this.girisButtonLabel = girisButtonLabel;
    }

    public void setUser(Person user) {
	  this.user = user;
    }

    public void setSayfaBox(String sayfaBox) {
	  this.sayfaBox = sayfaBox;
    }

    public void setUsername(String username) {
	  this.username = username;
    }

    public void setBox(boolean box) {
	  this.box = box;
    }

    public void setPassword(String password) {
	  this.password = password;
    }

    public void setEventModel(ScheduleModel eventModel) {
	  this.eventModel = eventModel;
    }

    public void setLogon(boolean logon) {
	  this.logon = logon;
    }

    public void setBoxTitle(String boxTitle) {
	  this.boxTitle = boxTitle;
    }

    public void setErrorMessage(String errorMessage) {
	  this.errorMessage = errorMessage;
    }

    public void setErrorLogin(boolean errorLogin) {
	  this.errorLogin = errorLogin;
    }

    public void setLoginPanel(boolean loginPanel) {
	  this.loginPanel = loginPanel;
    }

    public void setSayfaalt(String sayfaalt) {
	  this.sayfaalt = sayfaalt;
    }
   
    public void clickGiris(){
	  if(loginPanel==false){
		loginPanel=true;
		girisButtonLabel="Kapat";
	  }
	  else{
		loginPanel=false;
		girisButtonLabel="Giriş";
	  }
    }
   
    public void clickSearch(){
	  sayfaalt="search.xhtml";
          status=false;
    }
    
    public void clickControlPanel(){
	  sayfaalt="controlPanel.xhtml";
    }
    
    public void logout(){
	  user=null;
	  logon=false;
	  loginPanel=false;
	  girisButtonLabel="Giriş";
	  sayfaalt="";
	  loginPanel=false;
	  errorLogin=false;
          username="";
          status=true;
          password="";
          poolId=100;
	  if(user==null)
              logon=false;
          else
              logon=true;
	  eventModel=new DefaultScheduleModel();
    }
    
    public void login(){
	  if(username.startsWith("st")){
		user=StudentManager.checkUserNamePassword(username, password);
	  }
	  else
		user=TeacherManager.checkUserNamePassword(username, password);
	  if(isLogin())
		errorLogin=true;
	  logon=errorLogin;
	  if(!errorLogin){
		errorMessage="Giriş yapılamadı.";
		loginPanel=true;
	  }
	  else{
		loginPanel=false;
                
		events();
		if(LoginManager.isTeacher())
			courses = TeacherManager.getTeachersCourses(LoginManager.getUser().getId());
		else
			courses = CourseManager.getStudentsCourse(LoginManager.getUser().getId());
                sayfaalt="first.xhtml";
	  }
	  
    }
    
    public boolean isLogin(){
        return user!=null;
    }
    
    public String welcomeText(){
	  return LoginManager.getWelcomeText();
    }
    
    public void clickMyClass(){
	  sayfaalt="courses.xhtml";
    }
    
    public void clickHomePage(){
	  sayfaalt="first.xhtml";
    }
    
    public String profilePicture(){
	  return LoginManager.getUser().getProfilePicture();
    }
    
    public boolean userIsTeacher(){
	  return LoginManager.isTeacher();
    }
    
    public void clickCourse(){
	  sayfaalt="course.xhtml";
          status=true;
    }
    
    public void clickTeacher(){
	  sayfaalt="teacher.xhtml";
          status=true;
    }
    
    public void clickHomework(){
	  try{
		sayfaalt="homework.xhtml";
                status=true;
		makeBoxFalse();
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(HomePageBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    public void clickMyHomeworks(){
	  sayfaalt="studentHomeworks.xhtml";
          status=true;
    }
    
    public void clickBoxHomeworks(){
	  sayfaBox="homeworks.xhtml";
	  boxTitle="Ödevler";
	  box=true;
          status=true;
    }
    
    public void clickMyAnnouncements(){
	  sayfaalt="myAnnouncements.xhtml";
          status=true;
    }
    
    public void clickMyEvents(){
	  sayfaalt="Independent-addEvent.xhtml";
    }
    
    public void clickForum(){
	  sayfaalt="forum.xhtml";
          status=true;
    }
    
    public void clickTopic(){
	  sayfaalt="topic.xhtml";
          status=true;
    }
    
    public void clickStudent(){
	  try {
		sayfaalt="student.xhtml";
                status=true;
		makeBoxFalse();
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	  } catch (IOException ex) {
		Logger.getLogger(HomePageBean.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    public void clickBoxStudents(){
	  sayfaBox="students.xhtml";
	  boxTitle="Öğrenciler";
	  box=true;
    }
    
    public void clickBoxAnswer(){
	  sayfaBox="answer.xhtml";
	  boxTitle="Cevap";
	  box=true;
    }
    
    public void clickNewHomework(){
	  sayfaalt="newHomework2.xhtml";
    }
    
    public Announcement[] studentAnnouncement(){
	  if(LoginManager.isTeacher())//TODO öğretmen olursa?
		return AnnouncementManager.getTeachersAnnouncements(LoginManager.getUser().getId());
	  return AnnouncementManager.getStudentsAnnouncements(LoginManager.getUser().getId());
    }
    
    public void events(){
	  Event[] studentsEvents=null;
	  if(LoginManager.isTeacher())
		studentsEvents=AnnouncementManager.getTeachersEvents(LoginManager.getUser().getId());
	  else
		studentsEvents = AnnouncementManager.getStudentsEvents(LoginManager.getUser().getId());
	  eventModel=new DefaultScheduleModel();
	  for (int i = 0; i < studentsEvents.length; i++) {
		Event event = studentsEvents[i];
		eventModel.addEvent(new DefaultScheduleEvent(event.getText(),event.getDate(), event.getDate()));
	  }
    }
    
    public void makeBoxFalse(){
	  box=false;
    }
    
    public Homework[] getStudentHomework(){
	  return HomeworkManager.getStudentsHomework(LoginManager.getUser().getId());
    }
    
    public Forum[] studentForum(){
	  if(LoginManager.isTeacher())
		return ForumManager.getTeacherForums(LoginManager.getUser().getId());
	  return ForumManager.getStudentForums(LoginManager.getUser().getId());
    }
    
    public Answer[] getStudentAnswer(){
	  return HomeworkManager.getStudentsAnswers(LoginManager.getUser().getId());
    }
    public void clickMyCommunitys(){
          sayfaalt="myCommunitys.xhtml";
          status=true;  
    }
    public void clickMyAdminCommunitys(){
          sayfaalt="myAdminCommunitys.xhtml";
          status=true;  
    }
    
    public void clickCreateNewCommunity(){
          sayfaalt="newCommunity.xhtml";
          status=true;
    }
    
    public void clickJoinCommunity(){
          sayfaalt="joinCommunity.xhtml";
          status=true;
    }
    
    public void clickBoxNewHomework(){
	  sayfaBox="newHomework.xhtml";
	  boxTitle="Yeni Ödev Ekle";
	  box=true;
          status=true; 
    }
    
    public void clickBoxNewAnnouncement(){
	  sayfaBox="newCourseAnnouncement.xhtml";
	  boxTitle="Yeni Duyuru Ekle";
	  box=true;
    }
    
    public void clickBoxNewEvent(){
	  sayfaBox="newCourseEvent.xhtml";
	  boxTitle="Yeni Etkinlik Ekle";
	  box=true;
    }
    
    public void clickBoxSendEmail(){
	  sayfaBox="newCourseMail.xhtml";
	  boxTitle="Mail Gönder";
	  box=true;
    }
    public void clickBoxGroupAddAnnounce(){
	  sayfaBox="Community-addAnnounce.xhtml";
	  boxTitle="Yeni Duyuru Ekle";
	  box=true; 
    }
    public void clickBoxGroupUpdateAnnounce(){
	  sayfaBox="Community-updateAnnounce.xhtml";
	  boxTitle="Duyuruları Düzenle";
	  box=true; 
    }
    public void clickBoxGroupDeleteAnnounce(){
	  sayfaBox="Community-deleteAnnounce.xhtml";
	  boxTitle="Duyuru Sil";
	  box=true; 
    }
    public void clickBoxGroupAddEvent(){
	  sayfaBox="Community-addEvent.xhtml";
	  boxTitle="Etkinlik Ekle";
	  box=true; 
    }
    public void clickBoxGroupUpdateEvent(){
	  sayfaBox="Community-updateEvent.xhtml";
	  boxTitle="Etkinlikleri Düzenle";
	  box=true; 
    }
    public void clickBoxGroupDeleteEvent(){
	  sayfaBox="Community-deleteEvent.xhtml";
	  boxTitle="Etkinlik Sil";
	  box=true; 
    }
    public void clickBoxGroupForum(){
	  sayfaBox="Community-forum.xhtml";
	  boxTitle="Forum";
	  box=true; 
    }
    public void clickBoxGroupMembers(){
	  sayfaBox="Community-members.xhtml";
	  boxTitle="Üyeler";
	  box=true; 
          
    }
    public void clickBoxGroupSendMail(){
	  sayfaBox="Community-sendMail.xhtml";
	  boxTitle="Mail Gönder";
	  box=true; 
    }
    public void clickBoxGroupDeleteGroup(){
	  sayfaBox="Community-deleteGroup.xhtml";
	  boxTitle="Grubu Kapat";
	  box=true; 
    }
    public void clickBoxGroupLeaveGroup(){
	  sayfaBox="Community-leaveGroup.xhtml";
	  boxTitle="Gruptan Çık";
	  box=true; 
    }
     public void clickIndependentAddAnnounce(){
          sayfaalt="Independent-addAnnounce.xhtml";
          status=true;
    }
    
}
